unit _config;
interface
uses IniFiles, Classes, SysUtils,Dialogs, Grids
  ,_types
  ;

{$I _consts.pas }

  type TAppConfigValue = class
      function asString:string;
      function asInt:integer;
      procedure _set(new_value:string);
      function _get:string;
    private
      value:String;
  end;

  type TAppConfig = class
    public
      constructor Create;
      procedure save;
      procedure load;
      function getValueFor(option:string;default:string = ''):string;
      procedure setValueFor(option:string;value:string);
      function getObjectFor(option:string;default:string = ''):TAppConfigValue;
    private
      var list:TStringList;
      var values : Array of TAppConfigValue;
  end;

  var data : TAppConfig;
  var ini:TIniFile;
  const inifile : string = 'sql2l.ini';
implementation

function TAppConfigValue.asString:string;
begin
  result := self._get;
  self.Free;
end;

function TAppConfigValue.asInt:integer;
begin
  try
    result := strtoint(self._get);
  except
    result := 0;
  end;
  self.Free;
end;

procedure TAppConfigValue._set(new_value:string);
begin
  self.value := new_value;
end;

function TAppConfigValue._get:string;
begin
  result := self.value;
end;


constructor TAppConfig.Create;
begin
  list := TStringList.Create;
end;

(*
 * Save configuration data to file
 *)
procedure TAppConfig.save;
begin
  list.SaveToFile(inifile);
end;

(*
 * Load configuration data from file
 *)
procedure TAppConfig.load;
begin
  if (FileExists(inifile)) then list.LoadFromFile(inifile);
end;

function TAppConfig.getValueFor(option:string;default:string = ''):string;
begin
  if (list.Values[option] = '')
    then result := default
    else result := list.Values[option];
end;

function TAppConfig.getObjectFor(option:string;default:string = ''):TAppConfigValue;
begin
  result := TAppConfigValue.Create();
  result._set(getValueFor(option,default));
end;

procedure TAppConfig.setValueFor(option:string;value:string);
begin
  list.Values[option] := value;
end;


initialization
  data := TAppConfig.Create;
  inifile := ExtractFilePath(paramstr(0))+inifile;
//  ini := TIniFile.Create(inifile);
  data.load;

finalization
  data.save;

end.
